package com.fenxiangle.yueding.common.widget.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.fenxiangle.yueding.R;

/**
 * Created by Home on 2018/9/3.
 */

public class FilterPopupWindow implements PopupWindow.OnDismissListener {
    @Override
    public void onDismiss() {
        backgroundAlpha(1f);
    }

    LayoutInflater layoutInflater;
    private Activity mActivity;

    public FilterPopupWindow(Activity activity) {
        this.mActivity = activity;
        layoutInflater = LayoutInflater.from(activity);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mActivity.getWindow().setAttributes(lp);
    }

    PopupWindow popupWindow;
    String sortType=null,sex=null,age=null,authentication=null;
    public void initPopupWindow(View view,FilterCallBack filterCallBack) {

        View popupWindowView = layoutInflater.inflate(R.layout.filter_popup, null);
        //内容，高度，宽度
        popupWindow = new PopupWindow(popupWindowView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.FILL_PARENT, true);
        popupWindow.setAnimationStyle(R.style.AnimationRightFade);
        //菜单背景色
//        ColorDrawable dw = new ColorDrawable(0xffffffff);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //宽度
        //popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
        //高度
        //popupWindow.setHeight(LayoutParams.FILL_PARENT);
        //显示位置

        TextView filter_sex_credit=popupWindowView.findViewById(R.id.filter_sex_credit);
        TextView filter_sex_grade=popupWindowView.findViewById(R.id.filter_sex_grade);
        TextView filter_sex_nearby=popupWindowView.findViewById(R.id.filter_sex_nearby);

        filter_sex_credit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sortType="0";
                filter_sex_credit.setSelected(true);
                filter_sex_grade.setSelected(false);
                filter_sex_nearby.setSelected(false);
            }
        });

        filter_sex_grade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sortType="1";
                filter_sex_credit.setSelected(false);
                filter_sex_grade.setSelected(true);
                filter_sex_nearby.setSelected(false);
            }
        });
        filter_sex_nearby.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sortType="2";
                filter_sex_credit.setSelected(false);
                filter_sex_grade.setSelected(false);
                filter_sex_nearby.setSelected(true);
            }
        });

        TextView filter_sex=popupWindowView.findViewById(R.id.filter_sex);
        TextView filter_sex_man=popupWindowView.findViewById(R.id.filter_sex_man);
        TextView filter_sex_woman=popupWindowView.findViewById(R.id.filter_sex_woman);
        filter_sex.setSelected(true);
        filter_sex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sex="";
                filter_sex.setSelected(true);
                filter_sex_man.setSelected(false);
                filter_sex_woman.setSelected(false);
            }
        });
        filter_sex_man.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sex="0";
                filter_sex.setSelected(false);
                filter_sex_man.setSelected(true);
                filter_sex_woman.setSelected(false);
            }
        });
        filter_sex_woman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sex="1";
                filter_sex.setSelected(false);
                filter_sex_man.setSelected(false);
                filter_sex_woman.setSelected(true);
            }
        });

        TextView filter_age_buxian=popupWindowView.findViewById(R.id.filter_age_buxian);
        TextView filter_age_eighteen=popupWindowView.findViewById(R.id.filter_age_eighteen);
        TextView filter_age_core=popupWindowView.findViewById(R.id.filter_age_core);
        TextView filter_age_thirty=popupWindowView.findViewById(R.id.filter_age_thirty);
        filter_age_buxian.setSelected(true);
        filter_age_buxian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                age="";
                filter_age_buxian.setSelected(true);
                filter_age_eighteen.setSelected(false);
                filter_age_core.setSelected(false);
                filter_age_thirty.setSelected(false);
            }
        });
        filter_age_eighteen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                age="0";
                filter_age_buxian.setSelected(false);
                filter_age_eighteen.setSelected(true);
                filter_age_core.setSelected(false);
                filter_age_thirty.setSelected(false);
            }
        });

        filter_age_core.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                age="1";
                filter_age_buxian.setSelected(false);
                filter_age_eighteen.setSelected(false);
                filter_age_core.setSelected(true);
                filter_age_thirty.setSelected(false);
            }
        });
        filter_age_thirty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                age="2";
                filter_age_buxian.setSelected(false);
                filter_age_eighteen.setSelected(false);
                filter_age_core.setSelected(false);
                filter_age_thirty.setSelected(true);
            }
        });
        TextView filter_identity_buxian=popupWindowView.findViewById(R.id.filter_identity_buxian);
        TextView filter_boss_identity=popupWindowView.findViewById(R.id.filter_boss_identity);
        TextView filter_da_identity=popupWindowView.findViewById(R.id.filter_da_identity);
        TextView filter_identity=popupWindowView.findViewById(R.id.filter_identity);
        filter_identity_buxian.setSelected(true);
        filter_identity_buxian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                authentication="";
                filter_identity_buxian.setSelected(true);
                filter_boss_identity.setSelected(false);
                filter_da_identity.setSelected(false);
                filter_identity.setSelected(false);
            }
        });

        filter_boss_identity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                authentication="1";
                filter_identity_buxian.setSelected(false);
                filter_boss_identity.setSelected(true);
                filter_da_identity.setSelected(false);
                filter_identity.setSelected(false);
            }
        });
        filter_da_identity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                authentication="2";
                filter_identity_buxian.setSelected(false);
                filter_boss_identity.setSelected(false);
                filter_da_identity.setSelected(true);
                filter_identity.setSelected(false);
            }
        });
        filter_identity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                authentication="3";
                filter_identity_buxian.setSelected(false);
                filter_boss_identity.setSelected(false);
                filter_da_identity.setSelected(false);
                filter_identity.setSelected(true);
            }
        });

        TextView filter_confirm=popupWindowView.findViewById(R.id.filter_confirm);
        TextView filter_reset=popupWindowView.findViewById(R.id.filter_reset);

        filter_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FilterInfo filterInfo=new FilterInfo();
                filterInfo.age=age;
                filterInfo.sex=sex;
                filterInfo.sortType=sortType;
                filterInfo.authentication=authentication;
                filterCallBack.Filter(filterInfo);
                popupWindow.dismiss();
            }
        });

        filter_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                filter_sex_credit.setSelected(false);
                filter_sex_grade.setSelected(false);
                filter_sex_nearby.setSelected(false);

                filter_sex.setSelected(true);
                filter_sex_man.setSelected(false);
                filter_sex_woman.setSelected(false);

                filter_age_buxian.setSelected(true);
                filter_age_eighteen.setSelected(false);
                filter_age_core.setSelected(false);
                filter_age_thirty.setSelected(false);


                filter_identity_buxian.setSelected(true);
                filter_boss_identity.setSelected(false);
                filter_da_identity.setSelected(false);
                filter_identity.setSelected(false);


                sortType=null;
                age=null;
                sex=null;
                authentication=null;
            }
        });

        //设置背景半透明
        backgroundAlpha(0.5f);
        //关闭事件
        popupWindow.setOnDismissListener(this);
        popupWindow.setWidth(getScreenWidth(mActivity)-300);
        popupWindow.showAtLocation(view, Gravity.RIGHT, 0, 500);
        popupWindowView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    popupWindow = null;
                }
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
                return false;
            }
        });
    }
    /**
     * 获取屏幕的高度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }


    public interface FilterCallBack{
        void Filter(FilterInfo filterInfo);
    }

  public   class FilterInfo{
        public String sortType;
        public String sex;
        public String age;
        public String authentication;

    }

}
